package com.bwie.house.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;


import com.bwie.house.conmap.Coordinate;
import com.bwie.house.conmap.UrlMap;
import com.bwie.house.service.impl.AddressServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.bwie.house.entity.CellEntity;
import com.bwie.house.service.CellService;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.R;



/**
 * 
 *
 * @author wangguohai
 * @email 270901876@qq.com
 * @date 2022-04-21 20:35:04
 */
@RestController
@RequestMapping("house/cell")
public class CellController {
    @Autowired
    private CellService cellService;

    @Autowired
    private AddressServiceImpl addressService;

    /**
    * @Author: 龍
    * @Description: 地图
    * @DateTime: 2022/5/9 11:43
    * @Params:
    * @Return:
    */
    @RequestMapping("/detailed/{cellId}")
    public String detailed(@PathVariable("cellId") Integer cellId){
        //查找到这个ID的信息
        CellEntity byId = cellService.getById(cellId);
        String s ="";
        if (byId!=null){
            //通过这个id的信息查找属于市的id
            Integer shiId = byId.getShiId();
            //再通过市的id查找到对应的市名称
            String addressName = addressService.getById(shiId).getAddressName();
            System.out.println("市："+addressName);
            //通过id再查找到小区的名称
            String cellName = byId.getCellName();
            //通过市名称+小区名称获取经纬度
            Coordinate coordinate = new Coordinate();
            String api = coordinate.api(addressName, cellName);
            System.out.println("api："+api);
            //通过经纬度调用静态地图第三方接口进行拼接
            s =  UrlMap.authorization(api);
        }
        return s;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cellService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{cellId}")
    public R info(@PathVariable("cellId") Integer cellId){
		CellEntity cell = cellService.getById(cellId);

        return R.ok().put("cell", cell);
    }

    /**
    * @Author: 龍
    * @Description: 小区保存
    * @DateTime: 2022/4/29 19:03
    * @Params:
    * @Return
    */
    @RequestMapping("/save")
    public R save(@RequestBody CellEntity cell){
        Date date = new Date();
        cell.setUpdateTime(date);
        cellService.save(cell);
        return R.ok();
    }

    /**
    * @Author: 龍
    * @Description: 小区更改
    * @DateTime: 2022/4/29 19:04
    * @Params:
    * @Return
    */
    @RequestMapping("/update")
    public R update(@RequestBody CellEntity cell){
        Date date = new Date();
        cell.setUpdateTime(date);
		cellService.updateById(cell);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] cellIds){
		cellService.removeByIds(Arrays.asList(cellIds));
        return R.ok();
    }

}
